package com.aw.guiguzi.module.setting.presenters

import com.aw.baselib.base.AwCommonPresenter
import com.aw.baselib.net.callback.AwApiCallback
import com.aw.guiguzi.bean.LoginResultBean
import com.aw.guiguzi.module.setting.views.ModifyPwdView
import com.aw.guiguzi.net.observers.NetObCommon
import com.aw.guiguzi.util.AppUserUtil.account
import com.aw.guiguzi.util.AppUserUtil.saveToken

/**
 * @author : hzw
 */
class ModifyPwdPresent(private val mView: ModifyPwdView.View) : AwCommonPresenter(),
    ModifyPwdView.Presenter {
    override fun verifyPwd(oldPwd: String?, newPwd: String?) {
        addIOSubscription(
            NetObCommon.login(account, oldPwd),
            object : AwApiCallback<LoginResultBean>(mView, true, true) {
                override fun onSuccess(result: LoginResultBean) {
                    saveToken(result.accessToken)
                    updatePwd(newPwd)
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    mView.showMsg(msg)
                }
            })
    }

    override fun updatePwd(newPwd: String?) {
        addIOSubscription(
            NetObCommon.updatePwd(newPwd),
            object : AwApiCallback<Boolean>(mView, true, true) {
                override fun onSuccess(result: Boolean) {
                    mView.updatePwdSuccess()
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    mView.showMsg(msg)
                }
            })
    }
}